package com.carl.zlds.service.impl;

import com.carl.zlds.tool.common.PageBO;
import com.carl.zlds.tool.util.AssertUtil;
import com.carl.zlds.tool.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carl.zlds.dao.AdMapper;
import com.carl.zlds.model.Ad;
import com.carl.zlds.model.Article;
import com.carl.zlds.pojo.dto.AdSearchDTO;
import com.carl.zlds.pojo.dto.SaveAdDTO;
import com.carl.zlds.pojo.vo.AdVO;
import com.carl.zlds.service.IAdService;
import com.carl.zlds.service.IArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author carl
 */
@Slf4j
@Service
public class AdServiceImpl extends ServiceImpl<AdMapper, Ad> implements IAdService {
    @Autowired
    private IArticleService articleService;

    @Override
    public void saveAd(Long id, SaveAdDTO dto) {
        Ad ad;
        if (id > 0) {
            Wrapper<Ad> wrapper = new QueryWrapper<Ad>().eq("id", id).eq("weight", 0);
            ad = this.baseMapper.selectOne(wrapper);
            AssertUtil.isNull("广告不存在", ad);
        } else {
            ad = new Ad();
        }

        if (dto.getArticleId() > 0) {
            Wrapper<Article> wrapper = new QueryWrapper<Article>().eq("id", dto.getArticleId()).eq("weight", 0);
            Article article = articleService.getOne(wrapper);
            AssertUtil.isNull("文章不存在", article);
        }

        ad.setTitle(dto.getTitle());
        ad.setImg(dto.getImg());
        ad.setUrl(dto.getUrl());
        ad.setType(dto.getType());
        ad.setGameId(dto.getGameId());
        ad.setArticleId(dto.getArticleId());

        if (id > 0) {
            this.baseMapper.updateById(ad);
        } else {
            this.baseMapper.insert(ad);
        }
    }

    @Override
    public void deleteAd(Long id) {
        Wrapper<Ad> wrapper = new QueryWrapper<Ad>().eq("id", id).eq("weight", 0);
        Ad ad = this.baseMapper.selectOne(wrapper);
        AssertUtil.isNull("广告不存在", ad);

        ad.setWeight(-1);
        this.baseMapper.updateById(ad);
    }

    @Override
    public Map<String, Object> getAdList(AdSearchDTO dto, PageBO pageBO) {
        String limitSql = " limit " + (pageBO.getPage() - 1) * pageBO.getLimit() + ", " + pageBO.getLimit();
        List<AdVO> list = this.baseMapper.getAdList(dto, limitSql);
        Integer count = this.baseMapper.countAdList(dto, "");

        Map<String, Object> map = new HashMap<>(2);
        map.put("list", list);
        map.put("count", count);

        return map;
    }

    @Override
    public List<AdVO> getAdListByWeb(AdSearchDTO dto) {
        return this.baseMapper.getAdListByWeb(dto);
    }

    @Override
    public AdVO getAdInfo(Long id) {
        Wrapper<Ad> wrapper = new QueryWrapper<Ad>().eq("id", id).eq("weight", 0);
        Ad ad = this.baseMapper.selectOne(wrapper);
        AssertUtil.isNull("广告不存在", ad);

        AdVO adVO = new AdVO();
        ObjUtil.copy(ad, adVO);
        return adVO;
    }
}
